//
//  LoginViewController.m
//  JiZhuiAn
//
//  Created by roger lin on 16/6/20.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    self.view.backgroundColor = backgroundGrayColor;
}

#pragma mark - 初始化UI控件 -
- (void)initializeUI
{
    self.view.backgroundColor = backgroundGrayColor;
    int deviceTextFieldHeight = screenHeight/12;
    int deviceImageHeight     = screenHeight/5;
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(screenWidth/2 - deviceImageHeight/2, 80, deviceImageHeight, deviceImageHeight)];

    headImageView.image = [UIImage imageNamed:@"180.png"];
    [self.view addSubview:headImageView];
    
    phoneNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:headImageView] + 10 , screenWidth - 40, deviceTextFieldHeight)];
    phoneNumberTextField.placeholder = @" 手机号";
    phoneNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumberTextField.backgroundColor = backgroundWhiteColor;
    phoneNumberTextField.delegate = self;
    phoneNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneNumberTextField];
    
    passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:phoneNumberTextField] - 5 , screenWidth - 40, deviceTextFieldHeight)];
    passwordTextField.placeholder = @" 密码";
    passwordTextField.secureTextEntry = YES;
    passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    passwordTextField.backgroundColor = backgroundWhiteColor;
    passwordTextField.delegate = self;
    [self.view addSubview:passwordTextField];
    

    loginButton = [[UIButton alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:passwordTextField] + 30 , screenWidth - 40, deviceTextFieldHeight)];
    [loginButton setBackgroundColor:backgroundGreenColor];
    loginButton.layer.cornerRadius = 5.0;
    loginButton.layer.masksToBounds = YES;
    [loginButton setTitleColor:LightGrayColor forState:UIControlStateDisabled];
    [loginButton setTitleColor:backgroundWhiteColor forState:UIControlStateNormal];

    [loginButton setTitle:@"登 录" forState:UIControlStateNormal];
//    [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:loginButton];
    
    switchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [switchButton setFrame:CGRectMake(screenWidth/2 - 60, screenHeight - 50, 120, 40)];
    [switchButton setTitle:@"没有账号，去注册" forState:UIControlStateNormal];
    [switchButton addTarget:self action:@selector(switchToRegisterView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:switchButton];
}

- (CGFloat)getNextControlHeight:(UIView *)view
{
    return view.frame.origin.y + view.frame.size.height ;
}

#pragma mark - 点击下方按钮切换至注册页面 -
- (void)switchToRegisterView
{
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = @"pageCurl";
    animation.subtype = kCATransitionFromLeft;
    [self.view.window.layer addAnimation:animation forKey:nil]; //翻页效果
   
    viewModel = [[RegisterViewModel alloc]init];
    registerView = [[RegisterViewController alloc]initWithViewModel:viewModel];
    [self presentViewController:registerView animated:YES completion:nil];
}

- (void)bindViewModel
{
    [self initializeUI];
    [super bindViewModel];
    @weakify(self);
    RAC(self.viewModel,phoneNumber) = phoneNumberTextField.rac_textSignal;
    RAC(self.viewModel,password) = passwordTextField.rac_textSignal;
    
    [RACObserve(self.viewModel, loginSuccess) subscribeNext:^(id x) {
        //登录状态改变了
        if ([x integerValue]) {

        }

    }];
    
    [self.viewModel.loginCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        self.appStorage.userName = self.viewModel.phoneNumber;
        self.appStorage.password = self.viewModel.password;
        self.appStorage.onLine   = YES;
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self.viewModel.getSpineTestDataCommand execute:nil];
        });
        
        MainViewController * mainViewController = [[MainViewController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = mainViewController;
        
    }];
    
    [self.viewModel.loginCommand.executing subscribeError:^(NSError *error) {
        [MBProgressHUD showQuickTipWithTitle:@"登录失败" withText:@"账号密码错误"];
    }];
    
    loginButton.rac_command = self.viewModel.loginCommand;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
